package ds.queue;

public class PriorityQ {

	private int maxSize;
	private long[] queArray;
	private int nItems;
	//--------------------------------------------------
	public PriorityQ(int maxSize)
	{
		this.maxSize = maxSize;
		queArray = new long[maxSize];
		nItems=0;
	}
	
	public void insert(long value)
	{
		int j;
		if (nItems == 0)
			queArray[nItems++] = value;
		else {
			for (j = nItems - 1; j >= 0; j--) {
				if (value > queArray[j])
					queArray[j + 1] = queArray[j];
				else
					break;
			}
			queArray[j + 1] = value;
			nItems++;
		}
	}
	
	public void insert2(long value){
		int i;
		for(i=0;i<nItems && queArray[i]>value;i++);
		for(int j=nItems-1;j>=i;j--)
			queArray[j+1] = queArray[j];
		queArray[i] = value; // 
		nItems++;
	}
	
	public long remove()
	{
		return queArray[--nItems];
	}
	
	public long peekMin()
	{
		return queArray[nItems-1];
	}
	
	public boolean isEmpty()
	{
		return nItems == 0;
	}
	
	public boolean isFull()
	{
		return nItems == maxSize;
	}
	
	public static void main(String[] args)
	{
		PriorityQ pq = new PriorityQ(5);
		pq.insert2(12);
		pq.insert(18);
		pq.insert2(2);
		pq.insert(6);
		pq.insert(7);
		
		while(!pq.isEmpty())
		{
			System.out.print(pq.remove()+" ");
		}
		
	}
}
